/**
 * 
 */
package es.aradesh.ministryassist.logic.ministry;

import android.content.ContentValues;

import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.db.HomeColumns;

/**
 * @author xmorales
 *
 */
public class Home implements MinistryElement{
	//This order has to be the same as is in the array home_status
	public final static int STATUS_NOT_AT_HOME = 0;
	public final static int STATUS_BUILDING = 1;
	public final static int STATUS_NO_INTEREST= 2;
	public final static int STATUS_BUSY = 3;
	public final static int STATUS_GIVED= 4;
	public final static int STATUS_RETURN= 5;
	
	// TODO: Not localized
	public final static String[] STATUS_ARRAY = {"NC", "ED", "NI", "O", "PUBLI", "VOLVER"};
	
	public final static int NOT_IN_BUILDING= -1;

	private final static int FLAT_INC=3;
	
	protected long id;
	protected Street street;
	protected Home building;
	protected Home[] flats;
	protected int levels;
	protected int doors;
	protected String name;
	protected int status;
	// TODO: Usar para guardar el estado localizado
	// private String localizedStatus;
	protected String owner;
	
	protected int nextFlat;
	protected int maxFlats;
	
//	public Home(long id, Street street, Home building, String name, int status, String owner){
//	}
	
/**
 * @param id
 * @param levels
 * @param doors
 * @param name
 * @param street
 */
	public Home(long id, int levels, int doors, String name, Street street) {
		this.id = id;
		this.levels = levels;
		this.doors = doors;
		this.name = name;
		this.street = street;
		
		this.nextFlat = 0;
		this.maxFlats = FLAT_INC;
		this.flats = new Home[maxFlats];
		this.status = STATUS_BUILDING;
		
		//All this do not apply
		this.building = null;
		this.owner = null;
		
	}
/**
	 * @param id
	 * @param street
	 * @param building
	 * @param name
	 * @param status
	 * @param owner
	 */
	public Home(long id, Street street, Home building, String name, int status,
			String owner) {
		this.id = id;
		this.street = street;
		this.building = building;
		this.name = name;
		this.status = status;
		//this.localizedStatus = "";
		this.owner = owner;
		
		//All this do not apply
		this.doors=0;
		this.levels=0;
		this.maxFlats=0;
		this.flats=null;
		this.nextFlat=-1;
	}
	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}
	/**
	 * @return the streetId
	 */
	public Street getStreet() {
		return street;
	}
	/**
	 * @return the buildingId
	 */
	public Home getBuilding() {
		return building;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}
	/**
	 * @return the owner
	 */
	public String getOwner() {
		return owner;
	}
	/**
	 * @return the levels
	 */
	public int getLevels() {
		return levels;
	}
	/**
	 * @return the flats
	 */
	public Home[] getFlats() {
		return flats;
	}
	/**
	 * @return the doors
	 */
	public int getDoors() {
		return doors;
	}
	/**
	 * @param doors the doors to set
	 */
	public void setDoors(int doors) {
		this.doors = doors;
	}
	/**
	 * @param levels the levels to set
	 */
	public void setLevels(int levels) {
		this.levels = levels;
	}
	/**
	 * @param flats the flats to set
	 */
	public void setFlats(Home[] flats) {
		this.flats = flats;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}
	/**
	 * @param street the street to set
	 */
	public void setStreet(Street street) {
		this.street = street;
	}
	/**
	 * @param building the building to set
	 */
	public void setBuilding(Home building) {
		this.building = building;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}
	/**
	 * @param status the status to set
	 */
	/*public void setLocalizedStatus(String description) {
		this.localizedStatus = description;
	}*/
	/**
	 * @param owner the owner to set
	 */
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public void addFlat(Home newFlat){
		if (nextFlat>=maxFlats){
			Home[] temp = new Home[maxFlats+FLAT_INC];
			for(int i = 0; i<maxFlats;i++) temp[i]=flats[i];
			maxFlats +=FLAT_INC;
			this.flats = temp;
		}
		this.flats[nextFlat] = newFlat;
		nextFlat++;
	}
	public Home getFlat(int position){
		return flats[position];
	}
	
	public boolean isBuilding(){
		return (this.status == STATUS_BUILDING);
	}
	public void refreshDB(DbAdapter mDb){
		ContentValues cv = new ContentValues(3);
		cv.put(HomeColumns.NAME, this.name);
		cv.put(HomeColumns.STREET_ID, this.street.getId());
		cv.put(HomeColumns.STATUS, this.status);
		if (this.building != null ) cv.put(HomeColumns.BUILDING_ID, this.building.getId());
		cv.put(HomeColumns.DOORS, this.doors);
		cv.put(HomeColumns.LEVELS, this.levels);
		cv.put(HomeColumns.OWNER, this.owner);
		mDb.updateById(DbAdapter.TABLE_HOME, this.id, cv);	
	}
	public String toString(){
		if (name.matches("[0-9]*"))
		return new Integer(this.name) + " (" + Home.STATUS_ARRAY[status] + ")";
		else return this.name + " (" + Home.STATUS_ARRAY[status] + ")"; 
	}
	/* (non-Javadoc)
	 * @see es.aradesh.ministryassist.logic.ministry.MinistryElement#getActivityEditType()
	 */
	public int getActivityEditType() {
		// TODO Auto-generated method stub
		return MinistryElement.ACTIVITY_EDIT_HOME;
	}
	public boolean delete(DbAdapter mDb) {
		if(this.isBuilding()){
			for(int i = 0; i<this.nextFlat;i++) flats[i].delete(mDb);
		}
		return mDb.removeById(DbAdapter.TABLE_HOME, this.id);
	}
	/* (non-Javadoc)
	 * @see es.aradesh.ministryassist.logic.ministry.MinistryElement#getParam(java.lang.String)
	 */
	public Object getParam(String parameter) {
		Object result = null;
		if(parameter == HomeColumns.NAME) result = this.name;
		else if(parameter == HomeColumns.STATUS) result = this.status;
		else if(parameter == HomeColumns.DOORS) result = this.doors;
		else if(parameter == HomeColumns.LEVELS) result = this.levels;
		else if(parameter == HomeColumns.OWNER) result = this.owner;
		return result;
	}
}
